//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

namespace Microsoft.Research.eResearch.Blackboard.Common
{
    using System;
    using System.Collections.Generic;
    using System.Globalization;
    using System.Linq;
    using System.Text;

    /// <summary>
    /// Represents a collection of <see cref="SubscriptionItem"/>s that the
    /// subscriber wishes to subscribe to.
    /// </summary>
    [Serializable]
    public class SubscriptionProfile : List<SubscriptionItem>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="SubscriptionProfile"/> class.
        /// </summary>
        public SubscriptionProfile()
            : base()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SubscriptionProfile"/> class.
        /// </summary>
        /// <param name="capacity">The capacity.</param>
        public SubscriptionProfile(int capacity)
            : base(capacity)
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="SubscriptionProfile"/> class.
        /// </summary>
        /// <param name="collection">The collection whose elements are copied to the new list.</param>
        /// <exception cref="T:System.ArgumentNullException">
        /// <paramref name="collection"/> is null.</exception>
        public SubscriptionProfile(IEnumerable<SubscriptionItem> collection)
            : base(collection)
        {
        }

        /// <summary>
        /// Gets the <see cref="Microsoft.Research.eResearch.Blackboard.Common.SubscriptionItem"/> with the specified name.
        /// If an item cannot be found with that name, null is returned.
        /// </summary>
        /// <param name="name">Name of the subscription item.</param>
        public SubscriptionItem this[string name]
        {
            get
            {
                return this.Where(item => item.Name == name).FirstOrDefault();
            }
        } 

        /// <summary>
        /// Returns a <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </summary>
        /// <returns>
        /// A <see cref="T:System.String"/> that represents the current <see cref="T:System.Object"/>.
        /// </returns>
        public override string ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine(String.Format(CultureInfo.CurrentUICulture, "SubscriptionProfile, Count={0}", this.Count));
            foreach (SubscriptionItem item in this)
            {
                sb.AppendLine(String.Format(CultureInfo.CurrentUICulture, " Name='{0}'", item.Name));
            }

            return sb.ToString();
        }
    }
}
